package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.ValidateCodeUtils;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderMes;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class WxOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 根据预约id查询预约信息
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id) {
        log.info("根据预约id查询预约信息:{}", id);
        Order order = orderService.findById(id);
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
    }


    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderMes ordermes) {
        log.info("预约信息:{}", ordermes);
        //随机生成4位验证码
        String validateCode = String.valueOf(ValidateCodeUtils.generateValidateCode(4));
        log.info("验证码:{}", validateCode);
        //将验证码存入redis中
        redisTemplate.opsForValue().set("ValidateCode", ordermes.getValidateCode());

        Integer id = orderService.submitOrder(ordermes);
        switch (id) {
            case -1:
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            case -2:
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            case -3:
                return new Result(false, MessageConstant.ORDER_FULL);
            case -4:
                return new Result(false, MessageConstant.HAS_ORDERED);
            default:
                return new Result(true, MessageConstant.ORDER_SUCCESS, id);
        }


    }
}
